<template>
  <div id="CollectionManagement">
        <div>
            <el-card class="box-card">
                <el-row>
                    <el-col :span="8"> 
                        <el-input
                        placeholder="请输入收藏类型名称"
                        v-model="collectionName"
                        clearable>
                        </el-input>
                    </el-col>
                    <el-col :span="2" :offset="1"><el-button icon="el-icon-search" @click="getCollectionTypeByPage" circle></el-button></el-col>
                    <el-col :span="2"><el-button type="primary" @click="addCollection" round>新增</el-button></el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-table
                            :data="collectionList"
                            border
                            style="width:100%">
                            <el-table-column
                            fixed
                            prop="id"
                            label="序号"
                            >
                            </el-table-column>
                            <el-table-column
                            prop="collectionName"
                            label="收藏类型名称"
                            >
                            </el-table-column>
                            <el-table-column
                            fixed="right"
                            label="操作"
                            width="200">
                            <template slot-scope="scope">
                                <el-button @click="collectionTypeView(scope.row)" type="text" size="small" >查看</el-button>
                                <el-button @click="collectionTypeEdit(scope.row)" type="text" size="small">编辑</el-button>
                                <el-button @click="collectionTypeDelete(scope.row)" type="text" size="small" style="color: red;">删除</el-button>
                            </template>
                            </el-table-column>
                        </el-table>
                    </el-col>
                </el-row>
                <el-row style="text-align: center;">
                    <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage4"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="10"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="page.total">
                    </el-pagination>          
                </el-row>
            </el-card>
            <!-- 新增收藏类型 -->
            <el-dialog title="新增收藏类型" :visible.sync="saveCollectionTypeVisible" @close="handleClose('collectionTypeForm')">
                <el-form :model="collectionTypeForm" :rules="rules" ref="collectionTypeForm">
                    <el-form-item label="收藏类型名称" :label-width="formLabelWidth"  prop="collectionName">
                    <el-input v-model="collectionTypeForm.collectionName" autocomplete="off" ></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="saveCollectionTypeVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveCollectionType('collectionTypeForm')">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 数据查看功能 -->
            <el-dialog title="查看详情" :visible.sync="viewCollectionTypeVisible">
                <el-form :model="viewCollectionTypeForm">
                    <el-form-item label="收藏类型名称" :label-width="formLabelWidth">
                        <el-input v-model="viewCollectionTypeForm.collectionName" autocomplete="off"  :disabled="true"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="viewCollectionTypeVisible = false">关闭</el-button>
                </div>
            </el-dialog>
            <!-- 数据修改功能 -->
            <el-dialog title="角色编辑" :visible.sync="editCollectionTypeVisible">
                <el-form :model="editCollectionTypeEdit">
                    <el-form-item label="收藏类型名称" :label-width="formLabelWidth">
                    <el-input v-model="editCollectionTypeEdit.collectionName" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editCollectionTypeVisible = false">取 消</el-button>
                    <el-button type="primary" @click="updateCollectionType">确 定</el-button>
                </div>
            </el-dialog>
        </div>
  </div>
  
</template>

<script>
const axios = require('axios');
export default {
   name: 'CollectionManagement',
        data(){
             //新增弹窗检查收藏类型名称是否为空
            var collectionName = (rule, value, callback) => {
                if(value===''){
                        callback(new Error('请输入收藏类型名称'));
                }
                else if(value!==''){
                    //新增弹窗检查收藏类型名称是否重复
                    axios.get("http://127.0.0.1:8082/collectionType/selectCollectionTypeName?collectionName="+value).then(res=>{
                        this.getCollcetionTypeList= res.data.obj
                        this.getCollcetionTypeList.forEach((item)=>{
                            this.getName=item.collectionName
                        })
                        if (value===this.getName) {
                            callback(new Error('该名称已录入'));
                        } else {
                            callback();
                        }
                    })
                }
            };
            return {
                //查询是否重复名称
                getName:'',
                //存放模糊查输入框输入的值
                collectionName:"",
                //分页全查存放的数组
                collectionList: [],
                //查询收藏名称集合
                getCollcetionTypeList:[],
                //新增弹框true弹出
                saveCollectionTypeVisible: false,
                //查看弹框true弹出
                viewCollectionTypeVisible:false,
                //修改弹框true弹出
                editCollectionTypeVisible:false,
                // 新增时使用到的form对象
                collectionTypeForm: {
                    id:"",
                    collectionName:""

                },
                //查看时使用到的form对象
                viewCollectionTypeForm:{
                    id:"",
                    collectionName:"",
                },
                // 修改时使用到的form对象
                editCollectionTypeEdit:{
                    id:"",
                    collectionName:"",
                },
                //弹窗行样式
                formLabelWidth: '120px',
                //分页当前选中页
                currentPage4: 1,
                page:{
                    pageSize:10,//条数
                    pageNum:1,//页数
                    total:0
                },
                tatal:0,
                defaultProps: {
                    children: 'childs',
                    label: 'menuName'
                },
                rules:{
                    collectionName :{validator: collectionName, trigger: 'blur'}
                }
                    
                
            }
        },
        methods:{
            //新增清空值
            handleClose(formName) {
                this.$refs[formName].resetFields()// 重置数据，同时也取消了表单的校验 refundFormData：el-form用ref绑定的值，
            },
            // 分页
            handleSizeChange(ps) {
                this.page.pageSize=ps;
                this.getCollectionTypeByPage()
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(pn) {
                this.page.pageNum=pn;
                this.getCollectionTypeByPage()
            },
            //收藏类型分页查+模糊查（收藏类型名称）
            getCollectionTypeByPage () {
                axios.get('http://127.0.0.1:8082/collectionType/getCollectionTypeByPage', {
                    params: {
                        collectionName: this.collectionName,
                        pageSize: this.page.pageSize,
                        pageNum: this.page.pageNum
                    }
                }).then((res) => {
                    this.collectionList = res.data.content
                    this.page.total = res.data.total
                })
                    
            },
            //新增收藏类型
            saveCollectionType(formName){ 
                var formdata = new FormData();
                formdata.set("collectionName",this.collectionTypeForm.collectionName)
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("http://127.0.0.1:8082/collectionType/saveCollectionType",formdata).then(res => {
                            if(res.data.obj>0){
                                this.message("恭喜你，保存数据成功！","success")
                                this.saveCollectionTypeVisible = false
                                this.collectionTypeForm={
                                    collectionName:""
                                }
                                this.getCollectionTypeByPage()
                            }else{
                                this.message("对不起，保存失败！","warning")
                            }
                        }).catch(error => {
                            this.$message.error('服务器崩溃了，请稍后再尝试使用！');
                        })
                    }else{
                        return false;
                    }
                });
            },
            //修改角色
            updateCollectionType(){
                var formdata = new FormData();
                formdata.set("id",this.editCollectionTypeEdit.id)
                formdata.set("collectionName",this.editCollectionTypeEdit.collectionName)
                axios.put("http://127.0.0.1:8082/collectionType/updateCollectionType",formdata)
                .then(res=>{
                    if(res.data.code==='200'){
                    this.message("恭喜你，修改数据成功！","success")
                            this.editCollectionTypeVisible = false
                            this.editCollectionTypeEdit={
                                collectionName:""
                            }
                            this.getCollectionTypeByPage();// 重新渲染数据列表
                    }else{
                            this.message("对不起，修改失败！","warning")
                        }
                    }).catch(error => {
                        this.$message.error('服务器崩溃了，请稍后再尝试使用！');
                })
            },
            //删除角色
            collectionTypeDelete(row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }//删除提示框
                ).then(() => {
                    axios.delete("http://127.0.0.1:8082/collectionType/collectionTypeDelete?id="+row.id)
                .then(res =>{
                    if(res.data.code==='200' ){
                        this.message("恭喜你，删除成功！","success")
                        this.getCollectionTypeByPage()
                    }else{
                        this.message('删除失败','warning')
                    }
                }).catch(error =>{
                        alert(error)
                })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            //消息提示框
            message(message,type){
                this.$message({
                    message:message,
                    type: type
                });
            },
            //查看按钮回显值
            collectionTypeView(row){
                this.viewCollectionTypeVisible = true
                this.viewCollectionTypeForm.collectionName=row.collectionName
            },
            //修改按钮回显值
            collectionTypeEdit(row){
                this.editCollectionTypeVisible=true
                this.editCollectionTypeEdit.id = row.id
                this.editCollectionTypeEdit.collectionName=row.collectionName
            },
            //新增按钮弹出弹框
            addCollection(){
                this.saveCollectionTypeVisible = true
            },
        },
        //初始化加载数据
        mounted(){
            this.getCollectionTypeByPage()//分页全查
        }, 
}
</script>
<style>
/**
* 附件上传样式
*/
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
.el-icon-arrow-down {
    font-size: 12px;
}
.box-card {
    height: 100%;
}
</style>